home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2960 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A Very Simple Socket Question
  5. Date: 24 Jan 1996 08:29:03 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4e5mofINNfjh@keats.ugrad.cs.ubc.ca>
  8. References: <4dfgj8$6p9@nntp.ucs.ubc.ca> <4doprj$9t8@spanky.pls.ov.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4doprj$9t8@spanky.pls.ov.com>,
  12. Fletcher.Glenn@ov.com <glenn@ov.com> wrote:
  13.  >In article 6p9@nntp.ucs.ubc.ca, evil@unixg.ubc.ca (Peter Pan) writes:
  14.  >>Please help! I am a C beginner.
  15.  >>
  16.  >>======================================
  17.  >>Client:
  18.  >>
  19.  >>int sd, addrlen;
  20.  >>struct sockaddr_in svaddr;
  21.  >>
  22.  >>char *data;
  23.  >>data = "abcdef";
  24.  >>
  25.  >>..... /* skip */
  26.  >>
  27.  >>sendto(sd, data, strlen(data), 0, &svaddr, &addrlen);
  28.  >                  ^^^^^^^^^^^^
  29.  >This will not send the terminating null character, use "strlen(data) + 1".
  30.  
  31. Or sizeof(data), in this case. That avoid searching for a zero.
  32.  
  33.  >>data = (char*) malloc( 100*sizeof(char) );
  34.  >>
  35.  >>recvfrom(sd2, data, sizeof(data), 0, &claddr, &addrlen);
  36.  >                     ^^^^^^^^^^^^
  37.  >data is a char * with a size of 4.  What you really want is 100.
  38.  >Even so, I expect you will block waiting for the remaining 93 chars.
  39.  
  40. Not true. The recvfrom() will exit as soon as it gets a datagram of any
  41. length. That's what the (here ignored) return value is for; to tell you how
  42. much has been read. A lot of devices under UNIX work this way. The request size
  43. of a read() or recv() is only a hint at how much you can take. The return value
  44. tells you how much you got. This is needed even for ordinary files for the
  45. occasion when you ask for more bytes than are remaining in the file.
  46. -- 
  47.  
  48.